let app = new Vue({
	el: "#app",
	data() {
		return {
			showrightchoose: false,
			showanswer: true,
			radio: '1',
			nextDisable: false,
			beforeDisable: true,
			currentPage: 1, //当前第几页
			pageSize: 20, //每页显示几条数据
			number: 0,
			right: 0,
			onegoal: 5,
			pageData: [], //单页数据
			Form: [],
			achievementForm: {

			},
			conditionForm:{
				type:""
			}
		}
	},
	methods: {
		init() {
			var $this = this;
			this.pageData = this.achievementForm.slice((this.currentPage - 1) * this.pageSize, this.currentPage *
				this.pageSize);
			this.pageData.forEach(function(item, i) {
				if (item.question_type == "单选") {
					item.picked_radio = $this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i));

				} else if (item.question_type == "多选") {
					if (JSON.parse($this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i))) == null ||
						JSON.parse($this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i))) == undefined) {
						item.pickedMany = [];
					} else {
						item.pickedMany = JSON.parse($this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i)));
					}
				}
				item.isRight = this.$cookies.get('isRight_' + (($this.currentPage - 1) * $this.pageSize + i));
			});
		},
		show() {
			this.showrightchoose = true
		},

		async readquestion() {
			this.remove()
			this.remove()
			this.remove()
			try {
				this.remove()
				let res = await axios.post('api/testpaperview/brushtestpaper', this.conditionForm);
				if (res.code === 200) {
					this.achievementForm = res.data
				} else {
					this.$toast("后端错误");
				}
			} catch (e) {
				this.$toast("系统错误");
			}
			for (item in this.achievementForm) {
				this.$set(this.achievementForm[item], "isRight", "")
				this.$set(this.achievementForm[item], "pickedMany", [])
				this.achievementForm[item].rightchoose = this.achievementForm[item].rightchoose.replace(/'/g, '')
				this.achievementForm[item].rightchoose = this.achievementForm[item].rightchoose.split(',')
			}
			this.init();
		},
		back(){
				var href = "../service/p_desktop.html"
				window.location.href = href;
		},
		doCheck(value, item, index) {
			if (item.questionType == "单选") {
				//单选
				this.achievementForm[index].answerResults = item.picked_radio

				if (item.picked_radio == item.rightchoose[0]) {
					item.isRight = true;
				} else {
					item.isRight = false;
				}

			} else if (item.questionType == "多选") {
				//多选 
				var arr = []
				for (i in item.pickedMany) {
					arr.push(item.pickedMany[i])
				}
				this.achievementForm[index].answerResults = item.pickedMany
				if (JSON.stringify(arr.sort()) == JSON.stringify(item.rightchoose.sort())) {
					item.isRight = true;
				} else {
					item.isRight = false;
				}
			}
			// 将结果放入缓存
			if (item.questionType == "单选") {
				this.$cookies.set('pickedd_' + index, item.picked_radio);
			}
			// } else if (item.questionType == "多选") {
			// 	this.$cookies.set('pickedd_' + index, JSON.stringify(item.pickedMany));
			// }
			this.$cookies.set('isRight_' + index, item.isRight);
			this.$cookies.set('isChose_' + index, true);
		},
		remove() {
			for (item in this.$cookies.keys()) {
				this.$cookies.remove(this.$cookies.keys()[item])
			}

		},
		prev() { //上一页
			this.showanswer = false
			if (this.currentPage > 1) {
				this.currentPage = this.currentPage - 1;
				this.nextDisable = false
				this.init();
			}
			if (this.currentPage == 1) {
				this.nextDisable = false
				this.beforeDisable = true

			}
		},
		next() { //下一页

			if (this.currentPage < this.allPageNum) {
				this.currentPage = this.currentPage + 1;
				this.beforeDisable = false
				this.init();

			}
			if (this.currentPage == this.allPageNum) {
				this.nextDisable = true
				this.showanswer = true
			}

		},

	},
	computed: {
		allPageNum: {
			get: function() {
				return parseInt((this.achievementForm.length + this.pageSize - 1) / this.pageSize);
			},
		}
	},

	mounted() {
		this.readquestion();

	},
	created() {
		var url = decodeURI(decodeURI(location.search));

		strs = url.split("=");
		this.conditionForm.type = strs[1] == undefined ? this.conditionForm.type : strs[1];
	}
});
